// pages/restConfirm/restConfirm.js
const app = getApp();
const QQMapWX = app.getQQMap();
const demo = new QQMapWX({
  key: 'LBOBZ-AFKCX-SDS4Z-7OY56-CPLTJ-DGBMU'
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [{
      id: 1,
      latitude: '',
      longitude: '',
    }],
  },
  getBackPublish: function (){
    let title = this.data.title;
    let address = this.data.address;
    let longitude = this.data.longitude;
    let latitude = this.data.latitude;
    app.globalData.title = title;
    app.globalData.address = address;
    app.globalData.longitude = longitude;
    app.globalData.latitude = latitude;

    if(this.data.cityCode){
      let cityCode = this.data.cityCode;
      app.globalData.cityCode = cityCode;
      if (app.globalData.isChangeRule){
        app.globalData.isChangeRule = false;
        wx.navigateBack({
          delta: 3
        })
      }else{
        wx.switchTab({
          url: '../publish/publish'
        })
      }
    }else{
      wx.navigateBack();
    }
    
  },
  getAddress : function (){
    let that = this;
    demo.reverseGeocoder({
      location: {
        latitude: this.data.latitude,
        longitude: this.data.longitude
      },
      success: function (res) {
        console.log(res);
        let address = res.result.address;
        app.globalData.address = address;
        that.setData({
          ['address']: address
        })
      },
      fail: function (res) {
      }
    });
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({
      title: options.title,
      distance: Math.round(options.distance),
      address: options.address,
      longitude: options.longitude,
      latitude: options.latitude,
      ['markers[0].latitude']: options.latitude,
      ['markers[0].longitude']: options.longitude  
    })
    if(options.cityCode) {
      this.setData({
        cityCode: options.cityCode,
      })
    }
    this.mapCtx = wx.createMapContext('mySearchMap');
    this.getAddress()
  }
})